<?php
require_once '../includes/functions.php';
check_login();

header('Content-Type: application/json');

// 获取POST数据
$data = json_decode(file_get_contents('php://input'), true);

if (!isset($data['currentPassword']) || !isset($data['newPassword'])) {
    http_response_code(400);
    echo json_encode(['error' => '参数错误']);
    exit;
}

$current_password = $data['currentPassword'];
$new_password = $data['newPassword'];

// 验证新密码长度
if (strlen($new_password) < 6) {
    http_response_code(400);
    echo json_encode(['error' => '新密码长度不能小于6个字符']);
    exit;
}

// 尝试修改密码
if (change_password($current_password, $new_password)) {
    echo json_encode(['success' => true]);
} else {
    http_response_code(400);
    echo json_encode(['error' => '当前密码错误']);
}
